/**
 * The Community Project
 * Copyright Team1 Productions
 * 
 * Joshua Amick
 * Stephen Cunningham
 * Thema Davis
 * Tomika Dawson
 * Lucie DeCristofaro
 * 
 * All Rights Reserved.
 */

package com.actions;

// Servlet
import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.time.FastDateFormat;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionServlet;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.business.global.BusinessManager;


//This is the Base object for all actions, this is so that global changes to actions can be
//made with out copying and pasting.
public class CommunityBaseAction extends Action {
	protected static Log _log = LogFactory.getLog(CommunityBaseAction.class);
	protected static final FastDateFormat ISO_DATE = FastDateFormat.getInstance("yyyy-MM-dd");
	protected static final FastDateFormat ISO_DATETIME = FastDateFormat.getInstance("yyyy-MM-dd hh:mm");

    protected WebApplicationContext wac = null;
    protected BusinessManager biz = null;
    
	public CommunityBaseAction(){
		super();
	}

    public void setServlet (ActionServlet actionServlet){
        super.setServlet(actionServlet);
        ServletContext servletContext = actionServlet.getServletContext();

        wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
        biz = (BusinessManager) wac.getBean("businessMgr");
    }
     
    protected BusinessManager getBusiness()
    {
        return (BusinessManager) wac.getBean("businessMgr");
    }
   
	public void binaryResponse(HttpServletResponse response, String contentType,
				String filename, byte[] data) throws IOException {
		response.setContentType(contentType);
		response.setHeader("Content-Disposition","attachment; filename=\"" + filename + "\";");
		response.setContentLength(data.length);
		ServletOutputStream os = response.getOutputStream();
		os.write(data);
		os.flush();
		os.close();
	}
	
	//Global date checking for helping with displaying dates in forms
	public boolean checkDate(String year, String month, String day){
		if(month.equals("04")||
				month.equals("06")||
				month.equals("09")||
				month.equals("11")){
			if(Integer.parseInt(day)<31&&Integer.parseInt(day)>0)return false;
			else return true;
		}
		else if(month.equals("02")){
			int intYear = Integer.parseInt(year);
			if(intYear>0){
				if((intYear%4)==0){
					if(Integer.parseInt(day)>0&&Integer.parseInt(day)<30)return false;
					else return true;
				}
				else{
					if(Integer.parseInt(day)>0&&Integer.parseInt(day)<29)return false;
					else return true;
				}
			}
			else{
				return true;
			}
		}
		else{
			if(Integer.parseInt(day)>0&&Integer.parseInt(day)<32)return false;
			else return true;
		}
	}

}
